package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserOrgRight;
import com.tsd.system.service.SysUserOrgRightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysUserOrgRightsys_user_org_right 控制层
 *
 * @author 15971
 * @date 2019/12/30 16:10:02
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "sys_user_org_right-sys_user_org_right", description = "sys_user_org_right的API描述")
public class SysUserOrgRightController extends BaseController {

    @Resource
    private SysUserOrgRightService sysUserOrgRightService;

    @ApiOperation(value = "根据id获取sys_user_org_right", notes = "根据id获取sys_user_org_right")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "sys_user_org_rightid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysUserOrgRightById.action", method = RequestMethod.POST)
    public HttpResult<SysUserOrgRight> getSysUserOrgRightById() {
        HttpResult<SysUserOrgRight> hr = new HttpResult<SysUserOrgRight>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            SysUserOrgRight sysUserOrgRight = sysUserOrgRightService.getSysUserOrgRightById(id);
            hr.setData(sysUserOrgRight);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除sys_user_org_right", notes = "根据id删除sys_user_org_right")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "sys_user_org_rightid", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysUserOrgRight.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysUserOrgRight() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            sysUserOrgRightService.deleteSysUserOrgRight(id, null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存sys_user_org_right", notes = "保存sys_user_org_right，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "sys_user_org_right", required = true, paramType = "body", dataType = "SysUserOrgRight")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysUserOrgRight.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysUserOrgRight() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysUserOrgRight t = JSON.parseObject(json, SysUserOrgRight.class);
            sysUserOrgRightService.saveSysUserOrgRight(t, null);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "批量保存sys_user_org_right", notes = "批量保存sys_user_org_right，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "sys_user_org_right", required = true, paramType = "body", dataType = "SysUserOrgRight")
    })
    @ResponseBody
    @RequestMapping(value = "/batchSaveSysUserOrgRight.action", method = RequestMethod.POST)
    public HttpResult<String> batchSaveSysUserOrgRight() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String arrayJson = HlpUtils.getString(map, "sysUserOrgRight");
            String userSid = HlpUtils.getString(map, "userSid");
            List<SysUserOrgRight> sysUserOrgRights = JSON.parseArray(arrayJson, SysUserOrgRight.class);
//            sysUserOrgRightService.batchDeleteSysUserOrgRight(userSid);
//            for (SysUserOrgRight sysUserOrgRight : sysUserOrgRights) {
//                sysUserOrgRightService.saveSysUserOrgRight(sysUserOrgRight, null);
//            }
            // 2020-09-27 改为批量操作
            sysUserOrgRightService.batchSaveSysUserOrgRight(userSid, sysUserOrgRights, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "分页查询sys_user_org_right", notes = "分页查询sys_user_org_right")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysUserOrgRights.action", method = RequestMethod.POST)
    public HttpResult<List<SysUserOrgRight>> findSysUserOrgRights() {
        HttpResult<List<SysUserOrgRight>> hr = new HttpResult<List<SysUserOrgRight>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            PageBean page = new PageBean(map);
            List<SysUserOrgRight> list = sysUserOrgRightService.findSysUserOrgRights(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "查询sys_user_org_right", notes = "查询sys_user_org_right")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
    })
    @ResponseBody
    @RequestMapping(value = "/findSysUserOrgRightByUserSid.action", method = RequestMethod.POST)
    public HttpResult<List<SysUserOrgRight>> findSysUserOrgRightByUserSid() {
        HttpResult<List<SysUserOrgRight>> hr = new HttpResult<List<SysUserOrgRight>>();
        try {
            Map<String, Object> map = super.getParams(request);
            String user_sid = HlpUtils.getString(map, "user_sid");
            List<SysUserOrgRight> list = sysUserOrgRightService.findSysUserOrgRightByUserSid(user_sid);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
